package com.zt.dev.numbersimulation.controller;


import com.zt.dev.common.ServerResponse;
import com.zt.dev.numbersimulation.pojo.NumberSimulation;
import com.zt.dev.numbersimulation.pojo.Performance;
import com.zt.dev.numbersimulation.service.NumberSimulationService;
import com.zt.dev.semiPhysicalEquipment.util.StrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName NumberSimulationController
 * @Description TODO
 * @Author 小张学长
 * @Date 2020/2/26 15:40
 **/
@Controller
@RequestMapping("/NumberSimulation")
@Slf4j
public class NumberSimulationController {

    @Autowired
    private NumberSimulationService simulationService;

    /**
     *数字仿真模型信息注册
     * @param simulation
     * @return
     */
    @RequestMapping(value = "/addNumberSimulation")
    @ResponseBody
    public ServerResponse addNumberSimulation(@RequestBody  NumberSimulation simulation){
        if(!StringUtils.isEmpty(simulation)){
            if(simulationService.selSimulationByMdelName(simulation.getModelName())!=null){
                return ServerResponse.createByErrorMessage("仿真模型名称已存在");
            }
        /*    simulation.setCrateTime(DateUtil.getNowDateShort());*/
            if(simulationService.addNumberSimulation(simulation)){
                int simulationId = simulation.getId();
                List<Performance> performanceList = simulation.getPerformanceList();
                String str="";
                for (Performance performance: performanceList
                     ) {
                    performance.setNumberId(simulationId);
                    if(simulationService.addPerformance(performance)){
                        //获取插入性能参数的id
                        int performanceId = performance.getId();
                        str+=performanceId+",";
                    }else{
                        return ServerResponse.createByErrorMessage("新增数字仿真模型失败");
                    }
                }
                String performanceIds = StrUtils.strReg(str);
                simulation.setPerformance(performanceIds);
                simulation.setId(simulationId);
                //修改数字仿真模型主要性质参数列
                if(simulationService.updateNumberSimulation(simulation) >0){
                    return ServerResponse.createByErrorMessage("新增数字仿真模型成功");
                }else{
                    return ServerResponse.createByErrorMessage("新增数字仿真模型失败");
                }
            }else{
                return ServerResponse.createByErrorMessage("新增数字仿真模型失败");
            }
        }else{
            log.error("参数为空{}",simulation);
            return ServerResponse.createByErrorMessage("参数为空");
        }
    }


    /**
     * 获取仿真模型名称
     * @return
     */
    @GetMapping("/getSimulationByMdelName")
    @ResponseBody
    public ServerResponse getSimulationByMdelName(){
        List<NumberSimulation> simulationList = simulationService.getSimulationByMdelName();
        if(!StringUtils.isEmpty(simulationList)){
            return ServerResponse.createBySuccess(simulationList);
        }else{
            return ServerResponse.createByErrorMessage("数据为空");
        }
    }

    /**
     *根据模型id获取模型主要性能
     * @param numberId
     * @return
     */
    @GetMapping("getPerformanceByNumberId")
    @ResponseBody
    public ServerResponse getPerformanceByNumberId(@RequestParam int numberId){
        List<Performance> performanceList = simulationService.getPerformanceByNumberId(numberId);
        if(!StringUtils.isEmpty(performanceList)){
            return ServerResponse.createBySuccess(performanceList);
        }else{
            return ServerResponse.createByErrorMessage("模型主要参数数据为空");
        }
    }


}
